﻿using MvcWebRole1.Models;
using System;
using System.Security.Principal;
using System.Collections.Generic;

namespace MvcWebRole1.Helpers
{
    public static class DateTimeHelper
    {
        public static DateTime AdjustTimeZone( this DateTime dt, int offset )
        {
            if (dt.Kind == DateTimeKind.Utc)
            {
                TimeSpan offsetTs = TimeSpan.FromHours(offset);
                DateTime dtTemp = dt + offsetTs;
                DateTime result = new DateTime( dtTemp.Ticks, DateTimeKind.Local );
                return result;
            }
            return dt;
        }
    }
    public static class BbsHelper
    {
        public static string GetZipCodeFromUser(IPrincipal user)
        {
            if (user == null) throw new ArgumentNullException("user");

            var name = user.Identity.Name;
            if (name.Contains("@"))
            {
                return name.Substring(0, name.IndexOf("@"));
            }
            else
            {
                return "";
            }
        }

        public static string GetNickNameFromUser(IPrincipal user)
        {
            if (user == null) throw new ArgumentNullException("user");

            var name = user.Identity.Name;
            if (name.Contains("@"))
            {
                return name.Substring(name.IndexOf("@") + 1);
            }
            else
            {
                return name;
            }
        }

        public static IEnumerable<string> SplitTagList(string tagList)
        {
            if (!String.IsNullOrWhiteSpace(tagList))
            {
                foreach (var token in tagList.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    var tag = token.Trim();
                    if (!String.IsNullOrWhiteSpace(tag))
                    {
                        yield return tag;
                    }
                }
            }
        }

        public static int GetFromMessageNumber(BbsAreaIndexModel model)
        {
            return GetFromMessageNumber(model.CurrentPage, model.PageSize);
        }

        public static int GetFromMessageNumber(int currentPage, int pageSize)
        {
            return (currentPage - 1) * pageSize + 1;
        }

        public static int GetToMessageNumber(BbsAreaIndexModel model)
        {
            return GetToMessageNumber(model.CurrentPage, model.PageSize, model.Messages.Count);
        }

        public static int GetToMessageNumber(int currentPage, int pageSize, int currentPageMessageCount)
        {
            return (currentPage - 1) * pageSize + currentPageMessageCount;
        }

        public static bool IsFirstPage(BbsAreaIndexModel model)
        {
            return IsFirstPage(model.CurrentPage);
        }

        public static bool IsFirstPage(int currentPage)
        {
            return currentPage == 1;
        }

        public static bool IsLastPage(BbsAreaIndexModel model)
        {
            return IsLastPage(model.CurrentPage, model.PageSize, model.Total);
        }

        public static bool IsLastPage(int currentPage, int pageSize, int totalMessageCount)
        {
            return currentPage == (totalMessageCount - 1) / pageSize + 1;
        }
    }
}